package com.hudongwx.godoffood.fragment;


import android.animation.ObjectAnimator;
import android.content.Intent;
import android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.Community_Activity.NewMessageActivity;
import com.hudongwx.godoffood.activity.home_activity.SearchActivity;
import com.hudongwx.godoffood.activity.myActivity.CommunityMsgActivity;
import com.hudongwx.godoffood.activity.myActivity.ServiceActivity;
import com.hudongwx.godoffood.fragment.communityFragment.EatingFragment;
import com.hudongwx.godoffood.fragment.communityFragment.LiveFragment;
import com.hudongwx.godoffood.fragment.communityFragment.RecommendFragment;
import com.hudongwx.godoffood.fragment.communityFragment.ShowDanFragment;
import com.hudongwx.godoffood.fragment.communityFragment.SportFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */

@ContentView(R.layout.fragment_community)
public class CommunityFragment extends Fragment {
    @ViewInject(R.id.fab_fl)
    private FrameLayout framelayout;

    @ViewInject(R.id.community_viewpager)
    private ViewPager viewPager;
    @ViewInject(R.id.community_tablayout)
    private TabLayout tabLayout;
    @ViewInject(R.id.fab_video)
    private FloatingActionButton video;
    @ViewInject(R.id.fab_image)
    private FloatingActionButton image;
    @ViewInject(R.id.fab_close)
    private  FloatingActionButton close;
    private  View view;
private String TAG="TAG";

    public CommunityFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null){
            view = x.view().inject(this, inflater, container);
            MyCommumityAdpter myCommumityAdpter=new MyCommumityAdpter(getFragmentManager());
            viewPager.setAdapter(myCommumityAdpter);
            tabLayout.setupWithViewPager(viewPager);


            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    if (state==0){
                        if (close!=null){
                            if (close.isSelected()){
                                ObjectAnimator.ofFloat(image, "translationX", -160f, 0f).setDuration(200).start();
                                ObjectAnimator.ofFloat(image, "translationY", -160f, 0f).setDuration(200).start();
                                ObjectAnimator.ofFloat(image, "rotation", 0f, 360f).setDuration(200).start();
                                ObjectAnimator.ofFloat(video, "translationX", -200f, 0f).setDuration(200).start();
                                ObjectAnimator.ofFloat(video, "rotation", 0f, 360f).setDuration(200).start();
                                ObjectAnimator.ofFloat(close,"rotation",0f,360f).setDuration(200).start();
                                close.setSelected(false);
                            }
                        }
                    }

                }
            });

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), NewMessageActivity.class);
                    startActivity(intent);
                }
            });
            video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), NewMessageActivity.class);
                    startActivity(intent);
                }
            });
                framelayout.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (MotionEvent.ACTION_DOWN==event.getAction()){
                            if (close!=null){
                                if (close.isSelected()){
                                    framelayout.setVisibility(View.GONE);
                                    ObjectAnimator.ofFloat(image, "translationX", -160f, 0f).setDuration(200).start();
                                    ObjectAnimator.ofFloat(image, "translationY", -160f, 0f).setDuration(200).start();
                                    ObjectAnimator.ofFloat(image, "rotation", 0f, 360f).setDuration(200).start();
                                    ObjectAnimator.ofFloat(video, "translationX", -200f, 0f).setDuration(200).start();
                                    ObjectAnimator.ofFloat(video, "rotation", 0f, 360f).setDuration(200).start();
                                    ObjectAnimator.ofFloat(close,"rotation",0f,360f).setDuration(200).start();
                                    close.setSelected(false);
                                }
                            }
                        }
                        return false;
                    }
                });


        }
        return view;
    }



    class MyCommumityAdpter extends FragmentPagerAdapter {
        Fragment[] fragments = new Fragment[5];
        public MyCommumityAdpter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    if (null==fragments[position]){
                        fragments[position]=new RecommendFragment();
                    }
                    break;
                case 1:
                    if (null==fragments[position]){
                        fragments[position]=new ShowDanFragment();
                    }
                    break;
                case 2:
                    if (null==fragments[position]){
                        fragments[position]=new EatingFragment();
                    }
                    break;
                case 3:
                    if (null==fragments[position]){
                        fragments[position]=new SportFragment();
                    }
                    break;
                case 4:
                    if (null==fragments[position]){
                        fragments[position]=new LiveFragment();
                    }
                    break;
            }
            return fragments[position];

        }

        @Override
        public int getCount() {
            return fragments.length;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            if (position==0){
                return "推荐";
            }else if (position==1){
                return "我来晒单";
            }else if (position==2){
                return "饮食养生";
            } else if (position==3){
                return "运动健身";
            }else{
                return "品质生活";
            }
        }
    }

 @Event(R.id.msg)
    private void goMsg(View view){
     Intent intent=new Intent(getContext(), CommunityMsgActivity.class);
     startActivity(intent);
 }


    @Event(R.id.fab_close)
    private void anim(View view) {
        if (view.isSelected()) {
            framelayout.setVisibility(View.GONE);
            ObjectAnimator.ofFloat(image, "translationX", -160f, 0f).setDuration(400).start();
            ObjectAnimator.ofFloat(image, "translationY", -160f, 0f).setDuration(400).start();
            ObjectAnimator.ofFloat(image, "rotation", 0f, 360f).setDuration(400).start();
            ObjectAnimator.ofFloat(video, "translationX", -200f, 0f).setDuration(400).start();
            ObjectAnimator.ofFloat(video, "rotation", 0f, 360f).setDuration(400).start();
            ObjectAnimator.ofFloat(view,"rotation",0f,360f).setDuration(400).start();
            view.setSelected(false);
        } else {
            framelayout.setVisibility(View.VISIBLE);
            ObjectAnimator.ofFloat(image, "translationX", 0f, -160f).setDuration(400).start();
            ObjectAnimator.ofFloat(image, "translationY", 0f, -160f).setDuration(400).start();
            ObjectAnimator.ofFloat(image, "rotation", 0f, 360f).setDuration(400).start();
            ObjectAnimator.ofFloat(video, "translationX", 0f, -200f).setDuration(400).start();
            ObjectAnimator.ofFloat(video, "rotation", 0f, 360f).setDuration(400).start();
            ObjectAnimator.ofFloat(view,"rotation",0f,360f).setDuration(400).start();
            view.setSelected(true);
        }
    }

    @Event(R.id.search_food)
    private void searchFood(View view){
        startActivity(new Intent(getActivity(), SearchActivity.class));
    }

}
